#ifndef TESTCONTAINER_H
#define TESTCONTAINER_H

#include <iostream>

#include "../persistencia/file.h"
#include "../persistencia/container.cpp"
#include "../persistencia/binarystream.h"
#include "../persistencia/record.h"
#include "../persistencia/keys/typekey.h"

#include <stdio.h>

class UIntKey : public TypeKey<u_int32_t> {
    public:
        UIntKey(){}
        UIntKey(u_int32_t id) : TypeKey<u_int32_t>(id) {}

        virtual u_int32_t hashcode(){
            return id;
        }
};

class TestRecordInt : public Serializable {
    public:
        TestRecordInt() { }

        TestRecordInt(u_int32_t id){
            key.set(id);
        }
        UIntKey& getKey(){
            return key;
        }
        virtual void read(BinaryStream& in) {
            key.read(in);
        }
        virtual void write(BinaryStream& out) {
            key.write(out);
        }
        virtual std::string toString() {
            std::stringstream sout;
            sout << key.get();
            return sout.str();
        }
    private:
        UIntKey key;
};

void doTestContainer() {

    u_int32_t pos = 0;

    {
        File file;
        file.create("container.bin", 64);

        Container<UIntKey, TestRecordInt> container(64);

        for (u_int32_t i = 0; i < 12; i++) {
            TestRecordInt rec(i);
            container.add(rec);
        }

        std::cout << container.toString() << std::endl;

        file.append(container);
    }

    {
        File file;
        file.open("container.bin", 64);

        Container<UIntKey, TestRecordInt> container(64);

        std::cout << container.toString() << std::endl;

        file.read(pos, container);

        std::cout << container.toString() << std::endl;

        UIntKey key(5);
        Nullable<TestRecordInt> res = container.get(key);

        if (res)
            std::cout << "true" << std::endl;
        else
            std::cout << "false" << std::endl;
    }

    remove("container.bin");
}

#endif // TESTCONTAINER_H
